package com.swiot.biz.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.apache.commons.lang3.StringUtils;
import com.swiot.common.annotation.Log;
import com.swiot.common.core.controller.BaseController;
import com.swiot.common.core.domain.AjaxResult;
import com.swiot.common.enums.BusinessType;
import com.swiot.common.utils.SecurityUtils;
import com.swiot.biz.domain.TemplateAlarm;
import com.swiot.biz.service.ITemplateAlarmService;
import com.swiot.common.utils.poi.ExcelUtil;
import com.swiot.common.core.page.TableDataInfo;

/**
 * 模板告警Controller
 * 
 * @author kingdom
 * @date 2025-01-10
 */
@RestController
@RequestMapping("/biz/alarm")
public class TemplateAlarmController extends BaseController
{
    @Autowired
    private ITemplateAlarmService templateAlarmService;

    /**
     * 查询模板告警列表
     */
    @PreAuthorize("@ss.hasPermi('biz:alarm:list')")
    @GetMapping("/list")
    public TableDataInfo list(TemplateAlarm templateAlarm)
    {
        startPage();
        List<TemplateAlarm> list = templateAlarmService.selectTemplateAlarmList(templateAlarm);
        return getDataTable(list);
    }



    /**
     * 查询模板告警列表
     */
    @PreAuthorize("@ss.hasPermi('biz:alarm:list')")
    @GetMapping("/query")
    public TableDataInfo query(TemplateAlarm templateAlarm)
    {
        startPage();
        List<TemplateAlarm> list = templateAlarmService.selectTemplateAlarmList(templateAlarm);
        return getDataTable(list);
    }

    /**
     * 导出模板告警列表
     */
    @PreAuthorize("@ss.hasPermi('biz:alarm:export')")
    @Log(title = "模板告警", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TemplateAlarm templateAlarm)
    {
        List<TemplateAlarm> list = templateAlarmService.selectTemplateAlarmList(templateAlarm);
        ExcelUtil<TemplateAlarm> util = new ExcelUtil<TemplateAlarm>(TemplateAlarm.class);
        util.exportExcel(response, list, "模板告警数据");
    }

    /**
     * 获取模板告警详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:alarm:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(templateAlarmService.selectTemplateAlarmById(id));
    }

    /**
     * 新增模板告警
     */
    @PreAuthorize("@ss.hasPermi('biz:alarm:add')")
    @Log(title = "模板告警", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TemplateAlarm templateAlarm)
    {
        // 参数检查
        if (StringUtils.isEmpty(templateAlarm.getDescription())) {
            return error("告警描述不能为空");
        }
        
        if (StringUtils.isEmpty(templateAlarm.getTemplateId())) {
            return error("模板ID不能为空");
        }
        
        if (StringUtils.isEmpty(templateAlarm.getAlarmType())) {
            return error("告警类型不能为空");
        }
        
        if (StringUtils.isEmpty(templateAlarm.getAlarmLevel())) {
            return error("告警级别不能为空");
        }
        
        if (templateAlarm.getDeptId() == null) {
            return error("公司ID不能为空");
        }
        
        // 设置默认状态
        if (StringUtils.isEmpty(templateAlarm.getAlarmStatus())) {
            templateAlarm.setAlarmStatus("0"); // 0-未处理
        }
        
        if (StringUtils.isEmpty(templateAlarm.getProcessStatus())) {
            templateAlarm.setProcessStatus("0"); // 0-未处理
        }
        
        return toAjax(templateAlarmService.insertTemplateAlarm(templateAlarm));
    }

    /**
     * 修改模板告警
     */
    @PreAuthorize("@ss.hasPermi('biz:alarm:edit')")
    @Log(title = "模板告警", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TemplateAlarm templateAlarm)
    {
        // 基础判空检查
        if (templateAlarm == null) {
            return error("模板告警不能为空");
        }
        
        if (StringUtils.isEmpty(templateAlarm.getId())) {
            return error("模板告警ID不能为空");
        }
        
        return toAjax(templateAlarmService.updateTemplateAlarm(templateAlarm));
    }

    /**
     * 删除模板告警
     */
    @PreAuthorize("@ss.hasPermi('biz:alarm:remove')")
    @Log(title = "模板告警", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(templateAlarmService.deleteTemplateAlarmByIds(ids));
    }

    /**
     * 处理告警
     */
    @PreAuthorize("@ss.hasPermi('biz:alarm:process')")
    @Log(title = "模板告警", businessType = BusinessType.UPDATE)
    @PutMapping("/process/{id}")
    public AjaxResult process(@PathVariable("id") String id, @RequestBody TemplateAlarm templateAlarm)
    {
        if (StringUtils.isEmpty(templateAlarm.getProcessRemark())) {
            return error("处理备注不能为空");
        }
        
        templateAlarm.setId(id);
        templateAlarm.setProcessStatus("1"); // 1-已处理
        templateAlarm.setAlarmStatus("1"); // 1-已处理
        templateAlarm.setProcessBy(SecurityUtils.getUsername());
        templateAlarm.setProcessTime(new Date());
        
        return toAjax(templateAlarmService.updateTemplateAlarmProcessStatus(templateAlarm));
    }

    /**
     * 忽略告警
     */
    @PreAuthorize("@ss.hasPermi('biz:alarm:ignore')")
    @Log(title = "模板告警", businessType = BusinessType.UPDATE)
    @PutMapping("/ignore/{id}")
    public AjaxResult ignore(@PathVariable("id") String id)
    {
        TemplateAlarm templateAlarm = new TemplateAlarm();
        templateAlarm.setId(id);
        templateAlarm.setProcessStatus("2"); // 2-已忽略
        templateAlarm.setAlarmStatus("2"); // 2-已忽略
        templateAlarm.setProcessBy(SecurityUtils.getUsername());
        templateAlarm.setProcessTime(new Date());
        
        return toAjax(templateAlarmService.updateTemplateAlarmProcessStatus(templateAlarm));
    }

    /**
     * 批量处理告警
     */
    @PreAuthorize("@ss.hasPermi('biz:alarm:batchProcess')")
    @Log(title = "模板告警", businessType = BusinessType.UPDATE)
    @PutMapping("/batchProcess")
    public AjaxResult batchProcess(@RequestBody TemplateAlarm templateAlarm)
    {
        if (StringUtils.isEmpty(templateAlarm.getProcessRemark())) {
            return error("处理备注不能为空");
        }
        
        templateAlarm.setProcessStatus("1"); // 1-已处理
        templateAlarm.setAlarmStatus("1"); // 1-已处理
        
        return toAjax(templateAlarmService.batchProcessTemplateAlarm(templateAlarm));
    }
}
